package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;


@Service

public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmealMapper.insertOnlySetmeal(setmeal);
        List<SetmealDish> dishes  =setmealDTO.getSetmealDishes();
           dishes.forEach(dish -> {dish.setSetmealId(setmeal.getId());});
           setmealDishMapper.insertOnlySetmealDish(dishes);

    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        ids.forEach(id->{
            Setmeal setmeal= setmealMapper.getById(id);
            if(StatusConstant.ENABLE==setmeal.getStatus()){
                throw  new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);

            }
        });
        ids.forEach(setmealId->{
            setmealMapper.deleteById(setmealId);
            setmealDishMapper.deleteBySetmealId(setmealId);
        });

    }

    @Override
    public SetmealVO selectSetmealById(Long id) {
             SetmealVO setmealVO=setmealMapper.selectSetmealById(id);
             return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        // 1. 更新套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateOnlySetmeal(setmeal);

        // 2. 删除旧关联菜品
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteSetmealDishById(setmealId);

        // 3. 插入新关联菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

            setmealDishes.forEach(setmealdish -> setmealdish.setSetmealId(setmealId));
            setmealDishMapper.insertOnlySetmealDish(setmealDishes);

    }




    @Override
    public void starOrStop(Integer status, Long id) {
        setmealMapper.starOrStop(status,id);
    }

    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}


